package xyz.xiaoshengfu.xrpc.exception;

/**
 * <p>Title: XRpcException</p>
 * <p>Description: xRPC自定义异常类 </p>
 * @author xiaoshengfu(2439323118@qq.com)
 * @date 2020年4月13日
 */
public class XRpcException extends Exception {

	private static final long serialVersionUID = 1L;

	public static final int TIMEOUT = 1;
	public static final int EXCEPTION = 2;
	public static final int ERROR = 3;
	public static final int NO_SERVICE_PROVIDER = 4;

	private Integer exceptionCode;

	public Integer getExceptionCode() {
		return exceptionCode;
	}

	public XRpcException(String serviceApplicationName, String interfaceName, Integer exceptionCode) {
		super(genExceptionMessage(serviceApplicationName, interfaceName, exceptionCode));
	}

	private static String genExceptionMessage(String serviceApplicationName, String interfaceName,
			Integer exceptionCode) {
		String message = serviceApplicationName + ":" + interfaceName + ":";
		if (exceptionCode == TIMEOUT) {
			message += "Request service interface timeout.";
		} else if (exceptionCode == EXCEPTION) {
			message += "Request service interface exception.";
		} else if (exceptionCode == ERROR) {
			message += "Request service interface error.";
		} else if (exceptionCode == NO_SERVICE_PROVIDER) {
			message += "No service provider.";
		}
		return message;
	}

}
